#include "TwOgreDirection3DVariable.h"

#include "TwOgreWindow.h"
#include "TwOgreWindowManager.h"

namespace TwOgre{
	Direction3DVariable::Direction3DVariable(Window *parent, const Ogre::String& name, bool readOnly, const Ogre::String& label, 
		const Ogre::Vector3& vector,	const Ogre::String& groupName, const Ogre::String& help) :
		Variable(parent, name, TW_TYPE_DIR3F), mVector3(vector)
	{
		mTweakVector3[0] = mVector3.x;
		mTweakVector3[1] = mVector3.y;
		mTweakVector3[2] = mVector3.z;

		if(readOnly)
		{
			TwAddVarRO(mParent->getTweakBar(), mName.c_str(), TW_TYPE_DIR3F, mTweakVector3, "");
		}
		else
		{
			TwAddVarRW(mParent->getTweakBar(), mName.c_str(), TW_TYPE_DIR3F, mTweakVector3, "");
		}

		if(label.compare("") != 0)
		{
			setLabel(label);
		}
		if(groupName.compare("") != 0)
		{
			setGroup(groupName);
		}
		if(help.compare("") != 0)
		{
			setHelp(help);
		}
	}

	Direction3DVariable::Direction3DVariable(Direction3DParams& params) :
		Variable(params.parent_, params.name_, params.type_), mVector3(params.value_)
    {
        if((params.getCB_ != NULL) && (params.setCB_ != NULL) && (params.data_ != NULL))
        {
            TwAddVarCB(mParent->getTweakBar(), mName.c_str(), params.type_, params.setCB_, params.getCB_, params.data_, "");
        }
        else
        {
		    if(params.readOnly_)
		    {
                TwAddVarRO(mParent->getTweakBar(), mName.c_str(), params.type_, &mVector3, "");
		    }
		    else
		    {
                TwAddVarRW(mParent->getTweakBar(), mName.c_str(), params.type_, &mVector3, "");
		    }
        }
		
        if(params.label_.compare("") != 0)
		{
			setLabel(params.label_);
		}
		if(params.group_.compare("") != 0)
		{
			setGroup(params.group_);
		}
		if(params.help_.compare("") != 0)
		{
			setHelp(params.help_);
		}
    }
	
    Direction3DVariable::~Direction3DVariable()
	{
		TwRemoveVar(mParent->getTweakBar(), mName.c_str());
	}

	const Ogre::Vector3& Direction3DVariable::getDirection3DValue() const
	{
		// Get the values from the control and store them in Ogre's Vector3
		mVector3 = Ogre::Vector3(mTweakVector3[0], mTweakVector3[1], mTweakVector3[2]);
		return mVector3;
	}

	void Direction3DVariable::setDirection3DValue(const Ogre::Vector3& vector)
	{
		// Convert Ogre's Vector3 into a float[3]
        mVector3 = vector;
        mVector3.normalise();
		mTweakVector3[0] = mVector3.x;
		mTweakVector3[1] = mVector3.y;
		mTweakVector3[2] = mVector3.z;
	}
}; // End Namespace
